<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-07-01
 * Time: 11:24
 * 项目孵化
 */

namespace app\api\controller\v1;


use app\api\model\ProjectHatch;
use app\api\model\ProjectHatchComment;
use app\api\model\ProjectHatchLook;
use LinCmsTp5\exception\ParameterException;

class Project
{
    /**
     * 项目孵化列表
     * @validate('PagingParameter')
     * @auth('项目孵化','项目孵化管理')
     */
    public function getProjectList($page,$limit,$title,$username,$phone)
    {
        $where = [];
        if($title) {
            $where[] = [ 'title', 'like', '%'.$title.'%' ];
        }
        if($username) {
            $where[] = [ 'username', 'like', '%'.$username.'%' ];
        }
        if($phone) {
            $where[] = [ 'phone', 'like', '%'.$phone.'%' ];
        }
        $query = ProjectHatch::where($where)
            ->with('comment')
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '未发布';
            } elseif($item['status'] == 2) {
                $item['status_name'] = '已发布';
            }
            if($item['agree_status'] == 1) {
                $item['agree_status_name'] = '审核中';
            } elseif($item['agree_status'] == 2) {
                $item['agree_status_name'] = '审核通过';
            } elseif($item['agree_status'] == 3) {
                $item['agree_status_name'] = '审核未通过';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 项目孵化详情
     */
    public function getProjectInfo($id)
    {
        return ProjectHatch::get($id);
    }

    /**
     * 修改项目孵化
     */
    public function updateProject($id,$agree_status,$unagree_cnt)
    {
        ProjectHatch::where('id',$id)->update([
            'agree_status' => $agree_status,
            'unagree_cnt' => $unagree_cnt
        ]);
        return writeJson(202, [], '操作成功！');
    }

        /**
     * 删除项目孵化项目
     */
    public function deleteProject($id)
    {
       $info = ProjectHatch::get($id);
       if($info) {
           ProjectHatch::destroy($id);
           ProjectHatchComment::destroy([
               'project_hatch_id' => $id
           ]);
           ProjectHatchLook::destroy([
               'project_hatch_id' => $id
           ]);
           return writeJson(203, [], '删除成功');
       } else {
           throw new ParameterException([
               'msg' => [
                   'msg' => '删除失败'
               ]
           ]);
       }
    }

    /**
     * 删除项目孵化项目
     */
    public function deleteProjectComment($id)
    {
        $info = ProjectHatchComment::get($id);
        if($info) {
            ProjectHatchComment::destroy($id);
            return writeJson(203, [], '删除成功');
        } else {
            throw new ParameterException([
                'msg' => [
                    'msg' => '删除失败'
                ]
            ]);
        }
    }
}